package com.demo.system;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @author: demo 当scope不写的时候默认为singleton
 * Date: 12-10-16
 * Time: 下午2:09.
 * jsp中获取spring的WebApplicationContext
 * 方法一
 * WebApplicationContext context = (WebApplicationContext) config.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
 * 方法二(JSP或者servlet中使用,强烈推荐)
 * WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
 * ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
 * 我们一般在servlet中，初始化context,也就是说web中,都会写个专门的servlet来进行系统的相应初始化工作.
 */
public class AppContextManage implements ApplicationContextAware {
	
    private static ApplicationContext appContext ;
    
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        appContext = applicationContext;
    }

    public static Object getBean(String beanName) {
        return appContext.getBean(beanName);
    }
}
